<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2019-04-11
 * Time: 13:16
 */

namespace app\api\controller;


use app\common\model\UserAction;
use app\service\MsgService;
use app\service\ThirdService;
use app\service\WechatService;
use think\Db;
use think\Exception;

class Third extends Base
{

    public function alipaycallback(){

    }

    public function processing(){
        $order_id=input('order_id');
        ThirdService::S()->processing($order_id);
        //判断该订单是否可拼三免一
    }

    public function sendCust(){
        $openid='onlxrwbLchNAGcHwrxTCdOrOW9WE';
        $re=WechatService::S()->send(WechatService::$msgType_text,$openid,"<a href='http://shop.xixi05.cn/Api/Third/sendCust'>你好呀</a>");
        dump($re);die;
    }

    public function test_send(){

        try{
            $data['first']='请查询您余额信息';
            $data['user_name']='刘洪';
            $data['money']=1900;
            $data['remark']='请及时查收';
            $re=MsgService::S()->sendTemp(MsgService::$tempType_NEW_USER_FANGWEN,1,4,$data,"");
            dump($re);die;
        }catch (Exception $ex){
            dump($ex->getMessage());
        }
    }


    //记录用户行为
    public function logaction(){
        $url="http://tjr.xixi05.cn/m/chat/pages/index/index";
        $refer_http=explode("m/",$url);
        $http=explode("?",$refer_http[1]);//隔断参数

        $send_data['first']="用户访问通知";
        $send_data['nickname']=$this->member['nickname'];
        $send_data['ziyuan']=config('shop_name');
        $send_data['remark']="用户访问通知";

        //查询是否存在该访问记录
        $log_where['uid']=$this->uid;
        $log_where['action']=$http[0];
        $actionList=UserAction::getOne($log_where,"id,num");
        if($actionList){ //存在新增次数
            $savelog['num']=$actionList['num']+1;
            $savelog['update_time']=date("Y-m-d H:i:s");//date("")
            UserAction::update($savelog,array('id'=>$actionList['id']));
        }else{
            $log_data['refer_id']=$this->member['refer_id'];
            $log_data['uid']=$this->uid;
            $log_data['action']=$http[0];
            $log_data['cname']=self::$url_array[$http[0]];
            $log_data['create_time']=date("Y-m-d H:i:s");
            $log_data['update_time']=date("Y-m-d H:i:s");
            $log_data['num']=1;
            UserAction::create($log_data);
        }
        if(!empty(self::$url_array[$http[0]])){ //存在
            $send_data['project']=self::$url_array[$http[0]];
            MsgService::S()->sendTemp(MsgService::$tempType_NEW_USER_FANGWEN,$this->mall_id,$this->member['refer_id'],$send_data,"");
        }else{
            if(empty($http[0])){ //
                $send_data['project']="商城首页";//self::$array[$http[1]];
                //dump($data);die;
                MsgService::S()->sendTemp(MsgService::$tempType_NEW_USER_FANGWEN,$this->mall_id,$this->member['refer_id'],$send_data,"");
            }

        }
        //查询改行为是否已记录
    }


    //用户访问记录
    public function access_records(){
        $uid=input('uid');
        if(!empty($uid)){
            $where['uid']=$uid;
        }
        $array=self::$url_array;
        $backList=array();
        $action=Db::name('user_action');
        $where['refer_id']=$this->uid;
        foreach ($array as $k=>$v){
            $where['action']=$k;
            $backList[$v]=$action->where($where)->sum('num');
        }
        $this->ajax(200,'success',$backList);
    }

    //用户访问列表
    public function access_list(){
        $where['refer_id']=$this->uid;
        $page=input("page");
        if($page<=1){
            $page=1;
        }
        $page_num=20;
        $list=Db::name("user_action")
            ->where($where)
            ->order('update_time','desc')
            ->page($page,$page_num)
            ->select();
        if($list){
            $list=$list->toArray();
        }
        foreach ($list as $k=>$v){
            $list[$k]['refer_name']=$this->member['nickname'];
            $list[$k]['refer_headimgurl']=$this->member['headimgurl'];
            $user_info=Db::name("user")->field("nickname,headimgurl")->where(array('id'=>$v['uid']))->find();
            $list[$k]['user_headimgurl']=$user_info['headimgurl'];//Db::name("user")->where(array('id'=>$v['uid']))->value('nickname');
            $list[$k]['user_name']=$user_info['nickname'];
        }

        $data['code']=200;
        $data['message']="success";
        $data['data']=$list;
        $data['page_count']=ceil(Db::name("user_action")
            ->where($where)
            ->count()/$page_num);
        echo json_encode($data);exit();
    }


    


}